package level02.two;


import level02.one.test.MyTest;

import java.io.*;
import java.util.Scanner;

public class BookDAO implements BookDAOInterface {

    //创建一个文件用来管理图书信息
    File file = new File("test_1.txt");

    @Override
    public void add(File file) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要存入的图书信息");
        System.out.println("图书编号，作者，出版社，出版日期，单价，数量");
        //接受输入的信息
        while (true) {
            String nextLine = scanner.nextLine().trim();
            //将信息通过，分割，获取图书编号
            String[] split = nextLine.split(",");
            //将这些信息写入到文件中
            if (split.length != 6) {
                System.out.println("输入格式不正确，请重新输入（需要6个字段，用逗号分隔）");
                continue;
            }
            //用于读取文件中的内容
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

            String line;
            Boolean flag = false;

            while ((line = bufferedReader.readLine()) != null) {
                String[] line1 = line.split(",");
                if (line1.length > 0 && line1[0].equals(split[0])) {
                    flag = true; // 找到重复编号
                    break; // 无需继续检查
                }
            }
            bufferedReader.close();
            if (flag) {
                System.out.println("编号重复，请重新输入");
            } else {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                bufferedWriter.write(nextLine);
                bufferedWriter.newLine();
                bufferedWriter.close();
                System.out.println("添加成功");
                break;
            }
        }
    }

    @Override
    public void modify(File file) throws Exception {
        System.out.println("请输入需要修改图书的编号");
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();

        //首先是读取文件test_1.txt中的内容
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        /*
        同时对同一个文件创建 BufferedReader（读）和 BufferedWriter（写）：
        new FileWriter(file) 会直接清空文件内容
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        */

        //先用内存缓存所有内容
        StringBuilder stringBuilder = new StringBuilder();

        String line;

        //读取文件内容，用stringBuilder来保存在内存中
        while ((line = bufferedReader.readLine()) != null) {

            //将读取的内容分割
            String[] split = line.split(",");

            //判断读取的图书编号和输入编号是否符合
            if (split[0].equals(nextLine)) {
                System.out.println("编号为:" + nextLine + "的图书信息为:" + line);
                while (true) {
                    System.out.println("请问你要修改的图书内容（作者，出版社，出版日期，单价，数量）,推出修改(exit)");
                    String nextLine1 = scanner.nextLine().trim();
                    if (nextLine1.equals("exit")) {
                        break;
                    }
                    switch (nextLine1) {
                        case "图书编号":
                            System.out.println("<UNK>");
                            break;

                        case "作者":
                            System.out.print("需要修改作者为:");
                            String nextLine2 = scanner.nextLine().trim();

                            //将修改的内容替换到文件中
                            split[1] = nextLine2;
                            break;

                        case "出版社":
                            System.out.print("需要修改出版社为:");
                            String nextLine3 = scanner.nextLine().trim();

                            //将修改的内容替换到文件中
                            split[2] = nextLine3;
                            break;

                        case "出版日期":
                            System.out.print("需要修改出版日期为:");
                            String nextLine4 = scanner.nextLine().trim();

                            //将修改的内容替换到文件中
                            split[3] = nextLine4;
                            break;

                        case "单价":
                            System.out.print("需要修改单价为:");
                            String nextLine5 = scanner.nextLine().trim();

                            //将修改的内容替换到文件中
                            split[4] = nextLine5;
                            break;
                        case "数量":
                            System.out.print("需要修改数量为:");
                            String nextLine6 = scanner.nextLine().trim();

                            //将修改的内容替换到文件中
                            split[5] = nextLine6;
                            break;
                        default:
                            System.out.println("输入错误,请重新输入");
                            break;
                    }
                    System.out.println("修改成功！");
                }
                /*
                修改了一行中的内容，需要将拆开的内容重新拼接为一串字符串
                 将修改后的内容重新拼接成一行
                 */
                line = String.join(",", split);
            }
            // 将（可能修改过的）行添加到内存缓存
            stringBuilder.append(line).append("\n");

        }
        //关闭读取流
        bufferedReader.close();

        //将内存中创建的stringBuilder写入文件
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(stringBuilder.toString());
        bufferedWriter.close();
        System.out.println("图书文件已更新");
    }

    @Override
    public void drop(File file) throws Exception {
        System.out.println("请选择你要删除的图书编号");
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();

        //读取文件中的信息
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        //创建一个stringBuilder用来接受读取到的信息
        StringBuilder stringBuilder = new StringBuilder();

        String line;
        while ((line = bufferedReader.readLine()) != null) {

            String[] split = line.split(",");

            if (split[0].equals(nextLine)) {
                System.out.println("该图书信息为:" + line);
                System.out.println("你确定要删除吗(是/否)");
                String nextLine1 = scanner.nextLine();

                switch (nextLine1) {
                    case "是":
                        System.out.println("删除成功");
                        break;

                    case "否":
                        //把修改后的内容写入stringBuilder中
                        stringBuilder.append(line);
                        stringBuilder.append("\r\n");
                        System.out.println("取消删除");
                        break;

                    default:
                        System.out.println("请输入--是/否");
                }

            } else {
                stringBuilder.append(line).append("\r\n");
            }

        }
        //写入文件中
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(stringBuilder.toString());
        bufferedWriter.close();
    }


    @Override
    public void message(File file) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println("------图书信息------");
            System.out.println(line);
        }
    }

}
